﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FacturIT.Service.Project;

namespace Factur_IT.Project
{
    public partial class projectlist : System.Web.UI.Page
    {
        int companyid;
        protected void Page_Load(object sender, EventArgs e)
        {
            bool loggedIn = FacturIT.Service.FacturITManager.Instance.IsLoggedID();

            if (loggedIn != true)
                Response.Redirect("~/Account/login.aspx");

            int userID = Convert.ToInt32(Session["UserID"]);
            string param = Request.Params["type"];
            List<ProjectModel> listmodel = new List<ProjectModel>();
            List<ProjectDTO> projectlist = new List<ProjectDTO>();
            List<ProjectDTO> projectlistBuilder = new List<ProjectDTO>();

            if (param.ToLower().Equals("mineactive"))
            {
                List<int> bedrijven = new List<int>();
                foreach (var item in FacturIT.Service.FacturITManager.Instance.ReadMyCompanies(userID))
                {
                    bedrijven.Add(item.CompanyID);
                }
                foreach (var item in bedrijven)
                {
                    string id = item.ToString();
                    
                    projectlistBuilder = FacturIT.Service.FacturITManager.Instance.ReadAllActiveProjectsFromCompany(Convert.ToInt32(id)).ToList();
                    projectlist = projectlist.Union(projectlistBuilder).ToList();
                }
                
            }
            if (param.ToLower().Equals("mineall"))
            {
                List<int> bedrijven = new List<int>();
                foreach (var item in FacturIT.Service.FacturITManager.Instance.ReadMyCompanies(userID))
                {
                    bedrijven.Add(item.CompanyID);
                }
                foreach (var item in bedrijven)
                {
                    string id = item.ToString();
                    projectlistBuilder = FacturIT.Service.FacturITManager.Instance.ReadAllProjectsFromCompany(Convert.ToInt32(id)).ToList();
                    projectlist = projectlist.Union(projectlistBuilder).ToList();
                }
            }
                
            foreach (var item in projectlist)
            {
                FillList(listmodel, item);
            }

            this.gvProjects.DataSource = listmodel;
            this.gvProjects.EmptyDataText = "Er werden geen projecten gevonden";
            this.gvProjects.DataBind();
        }

        public void FillList(List<ProjectModel> lijst, ProjectDTO item)
        {
            lijst.Add(new ProjectModel()
            {
                ProjectID = item.ProjectID,
                Name = item.ProjectName,
                StartDate = item.StartDate,
                EndDate = item.EndDate,
                Customer = FacturIT.Service.FacturITManager.Instance.ReadCompany(item.Customer).CompanyName
            });        
        }

        protected void gvProjects_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string projectid = e.Row.Cells[0].Text;

                e.Row.Attributes.Add("onclick", "window.location=\" ./projectDetails.aspx?ProjectID=" + projectid+"\"");
            }
        }
    }

    public class ProjectModel
    {
        public int ProjectID { get; set; }
        public string Name { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime? EndDate { get; set; }
        public string Company { get; set; }
        public string Customer { get; set; }
    }
}